package com.xyyl.patient.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xyyl.patient.common.easemob.EasemobApiService;
import com.xyyl.patient.common.utils.SysConfigUtils;
import com.xyyl.patient.core.constant.AppEnum;
import com.xyyl.patient.core.controller.BaseController;
import com.xyyl.patient.core.support.Pager;
import com.xyyl.patient.core.util.ResponseBodyResult;
import com.xyyl.patient.core.util.RestClient;
import com.xyyl.patient.entity.base.NotificationEntity;
import com.xyyl.patient.entity.doctor.Doctor;
import com.xyyl.patient.entity.question.QuickQuestion;
import com.xyyl.patient.service.doctor.DoctorService;
import com.xyyl.patient.service.question.QuickQuestionService;
import com.xyyl.patient.vo.QuestionVo;

/**
 * @ClassName: QuickQuestionController
 * @Description: 用户快速提问Controller
 * 1.添加图片上传
 * 2.问题提交
 * 3.我的提问
 * @author fangsheng  
 * @date 2017年8月16日
 *
 */
@RestController
@RequestMapping("/quickquestion")
public class QuickQuestionController extends BaseController {

	private Logger logger=LogManager.getLogger(QuickQuestionController.class);
	  
	@Autowired
	private QuickQuestionService quickQuestionService;
	
	@Value("#{easemob['doctor_im_preffix']}")
	private String doctor_im_preffix;
	

	@Autowired
	private DoctorService doctorService;
	/**
	 * @Title: queryQuestionList
	 * @Description: 用户提问查询分页
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/queryQuestionList", method = RequestMethod.POST)
	public JSONObject queryQuestionList(@RequestParam Integer questionerid ) {
		
		logger.info("access method {}","queryQuestionList");
		logger.info("userid",questionerid);
		
	//	Pager pager=super.initPage();
		
		try {
			return ResponseBodyResult.success(quickQuestionService.queryQuestionList(questionerid));
			
		} catch (Exception e) {
			return ResponseBodyResult.error("问题列表查询失败!",e);
		
		}
	}
	
	
	/**
	 * @Title: insertQuestion
	 * @Description: 新增快速提问
	 * @param @param Questionid
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/insertQuestion", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertQuestion(
			
			@RequestParam(value = "questionbody") String questionbody,
			@RequestParam(value="questionfiles") String questionfiles,
			@RequestParam Integer archivesid,
			@RequestParam Integer questionerid
			) {
		
		logger.info("access method {}","insertQuestion");
		logger.info("questionerid",questionerid);
		
		QuestionVo map =new QuestionVo();
		try {
			
	           map.setQuestionbody(questionbody);
	           map.setQuestionfiles(questionfiles);
	       
	           map.setArchivesid(archivesid);
	           map.setQuestionerid(questionerid);
	           
			return ResponseBodyResult.success("提问成功",quickQuestionService.insert(map));
		} catch (Exception e) {
			return ResponseBodyResult.error("快速提问失败!",e);
		}
	}
	/**
	 * @Title: getFreeDoctor
	 * @Description: 免费咨询
	 * @param @param id
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/getFreeDoctor", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject getFreeDoctor(@RequestBody QuickQuestion question) {
		Pager pager = super.initPage();
		try {
			quickQuestionService.insert(question);
			pager.getPageData ().put("isrecomm",1);
			pager.getPageData().put("isrecommtop",1);
			pager.getPageData().put("pageNo",1);
			pager.setPageSize(99);
			List<Doctor> doctorList = doctorService.getDoctor(pager);

			ArrayList<String> temp=new ArrayList<String>();
			//String a= doctorService.getDoctorIMEI(138);

			List<Doctor> doctorList1 = new ArrayList<>();
			doctorList.forEach(
					doctor -> {
						String resule = EasemobApiService.getUserStatus(doctor_im_preffix+doctor.getId());
						

						if(resule.equals("online")){
							doctorList1.add(doctor);
							if(doctorService.getDoctorIMEI(doctor.getId())!=null){
								temp.add(doctorService.getDoctorIMEI(doctor.getId()));
							}
						}
					}
			);
			if(!temp.isEmpty()){
				NotificationEntity notificationEntity=null;	
				String activity=SysConfigUtils.getConfigValue(AppEnum.sysConfig.OMS_QUESTION_ORDER_ANDROID_ACTIVITY.name());
				String push_api_url=SysConfigUtils.getConfigValue(AppEnum.sysConfig.OMS_SYSTEM_PUSH_API_URL.name());
				notificationEntity= new NotificationEntity(temp, "免费咨询", "您有一条咨询订单，点击查看",activity) ;	    
				RestClient.postForObject(push_api_url, JSON.toJSONString(notificationEntity),String.class);
			}
			
			
			
			if(doctorList1.size()>0){
				return ResponseBodyResult.success("OK",1);
			}else {
				return ResponseBodyResult.success("没有在线免费医生",0);
			}
		} catch (Exception e) {
			return ResponseBodyResult.error("医生列表查询失败!", e);

		}

	}
	/**
	 * @Title: myDoctorList
	 * @Description: 我的医生列表
	 * @param @param id
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/myDoctorList", method = RequestMethod.GET)
	public JSONObject myDoctorList(Long id) {
		
		return ResponseBodyResult.success("OK");
	}
	
	/**
	 * @Title: myServiceList
	 * @Description: 我的服务列表
	 * @param @param id
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/myServiceList", method = RequestMethod.GET)
	public JSONObject myServiceList(Long id) {
		
		return ResponseBodyResult.success("OK");
	}
	
	/**
	 * @Title: myAccount
	 * @Description: 我的账户
	 * @param @param id
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/myAccount", method = RequestMethod.GET)
	public JSONObject myAccount(Long id) {
		
		return ResponseBodyResult.success("OK");
	}
	
	/**
	 * @Title: myprescriptionlist
	 * @Description:我的处方类别
	 * @param @param id
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/myPrescriptionList", method = RequestMethod.GET)
	public JSONObject myprescriptionlist(Long id) {
		
		return ResponseBodyResult.success("OK");
	}
	
	/**
	 * @Title: feedback
	 * @Description:问题反馈
	 * @param @param id
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/feedback", method = RequestMethod.POST)
	public JSONObject feedback(Long id) {
		
		return ResponseBodyResult.success("OK");
	}
	
	/**
	 * @Title: feedback
	 * @Description: 账户充值
	 * @param @param id
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/pay", method = RequestMethod.POST)
	public JSONObject pay(Long id) {
		
		return ResponseBodyResult.success("OK");
	}
	
	/**
	 * @Title: queryFinishOrderList
	 * @Description:查询已完成的订单信息集合
	 * @param @param type1
	 * @param @param type2
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/queryFinishOrderList", method = RequestMethod.GET)
	public JSONObject queryFinishOrderList(@RequestParam("type1") String type1, @RequestParam("type2") String type2) {

		logger.info(" query para:{}", type1);
		try {
			return ResponseBodyResult.success(quickQuestionService.queryFinishOrderList(type1,type2));
		} catch (Exception e) {
			return ResponseBodyResult.error("查询订单明细失败!", e);
		}
	}
}
